package com.park.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.RoleModel;
import com.park.client.service.user.RoleService;
import com.park.client.vo.MenuVo;
import com.park.common.constant.Constant;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

@Controller
@RequestMapping("/admin/role")
public class AdminRoleController extends BaseController{

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public PageData<RoleModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<RoleModel> pageData = new PageData<>();
		RoleModel role = new RoleModel();
		Integer total = roleService.selectTotal(role);
		Page page = new Page(pageNow,total,rows);
		role.setStartPos(page.getStartPos());
		role.setPageSize(page.getPageSize());
		List<RoleModel> list = roleService.selectPagination(role);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("roleList")
	public String roleList(HttpServletRequest request){
			return Global.ADMIN_PATH+"role/roleList";
	}
	
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result<String> save(RoleModel role,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.insertSelective(role);
		if(success){
			json.setMessage(Constant.SUCCESS_SAVE_MSG);
		}else{
			json.setSuccess(false);
			json.setMessage(Constant.FAIL_SAVE_MSG);
		}
		return json;
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<String> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			json.setSuccess(true);
		}else{
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
	
	/**
	 * 修改角色
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public Result<String> edit(RoleModel role,HttpServletRequest request){
		Result<String> json = new Result<>(true);
		boolean success = roleService.updateByPrimaryKeySelective(role);
		if(success){
			json.setMessage(Constant.SUCCESS_UPDATE_MSG);
			json.setSuccess(true);
		}else{
			json.setMessage(Constant.FAIL_UPDATE_MSG);
			json.setSuccess(false);
		}
		return json;
	}
	
	/**
	 * 用户页面角色信息
	 * */
	@RequestMapping("listRoles")
	@ResponseBody
	public List<RoleModel> listRoles(){
		return roleService.selectAll();
	}
	
	@RequestMapping("saveOrEditAuthority")
	@ResponseBody
	public Result<String> saveOrEditAuthority(@RequestBody MenuVo menus){
		Result<String> obj = new Result<>(true);
		
		return obj;
	}
	
}
